package com.blb.controller;

import com.blb.entity.Order;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.ILaserService;
import com.blb.service.IOrderService;
import com.blb.service.IPrinterService;
import com.blb.service.IProductionLineService;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RequestMapping("/pendingOrder")
@Controller
public class PendingOrderController {
    @Autowired
    private IProductionLineService iProductionLineService;
    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private IPrinterService iPrinterService;

    @Autowired
    private ILaserService iLaserService;
    /**
     * 待生产订单表
     * @param
     * @return
     */
    @ResponseBody
    @GetMapping("page")
    public BlbPage page(String orderId, int start, int length,HttpSession session) {
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<Map<String,Object>> orders = iOrderService.PendingOrder(start, length, orderId,companyId);
        int count = iOrderService.count2(orderId,companyId);
        return new DataTablePage(count, count, orders);
    }

    /**
     *  加到激光码管理列表
     */
    @PostMapping("updatePendingOrder")
        private void updatePendingOrder(@RequestBody Order order,HttpSession session) {
        System.out.println("order111111"+order);
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        //订单编号
        String orderId = order.getOrderId();
        //打印机
        String printer = order.getPrinter();
        //生产线名称查生产线id
        String productLineName = order.getProductLineId();
        //生产时间
        String productTime = order.getProductTime();
        //System.out.println("生产线名称查生产线id"+id);
        Integer lineId = iProductionLineService.findProductionLineIdByName(productLineName,companyId);
        //修改激光码状态
        iOrderService.updateLStats(orderId,companyId);
        iOrderService.updatePendingOrder(orderId,productTime,printer,lineId,companyId);
    }
    /**
     * 所有的生产线名称
     * @param model
     * @return
     */
    @RequestMapping("/findProductionLineName")
    @ResponseBody
    public List findProductionLineName(Model model,HttpSession session) {
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<String> productionLineName = iProductionLineService.findProductionLineName(companyId);
        model.addAttribute("productionLineName", productionLineName);
        return productionLineName;
    }
    /**
     *所有的打印机名称
     * @param model
     * @returnz
     */
    @RequestMapping("/findAllPrinterName")
    @ResponseBody
    public List findAllPrinterName(Model model,HttpSession session) {
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<String> allPrinterName = iPrinterService.findAllPrinterName(companyId);
        model.addAttribute("allPrinterName", allPrinterName);
        return allPrinterName;
    }
}